package com.itboye.mengwu.app.ui.mymeng;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.R.raw;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.GoodsAskQueryModel;
import com.itboye.mengwu.data.model.RedBagModel;
import com.itboye.mengwu.data.presenter.RedBagPresenter;

public class DaiJinQuanYiGuoQi extends BaseFragment implements Observer {
	ListView old_red_bag_list;
	MyAdapter adapter;
	int status;
	ArrayList<RedBagModel> arrayList=new ArrayList<RedBagModel>();
	private RedBagPresenter bagPresenter;
	public DaiJinQuanYiGuoQi(int status){
		this.status=status;
	}
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.list_red_bag;
	}

	@Override
	public void onMyActivityCreated() {
		bagPresenter=new RedBagPresenter(this);
		RedBagModel model=new RedBagModel();
		model.setUid((String) SPUtils.get(getActivity(), null, SpPublicParams.UID, ""));
		model.setUse_status(status+"");
		bagPresenter.getInfo(model);
	}

	class MyAdapter extends BaseAdapter {
		Context context;
		ArrayList<RedBagModel> arrayList;
		public MyAdapter(Context context,ArrayList<RedBagModel> arrayList) {
			this.context = context;
			this.arrayList=arrayList;
		}

		@Override
		public int getCount() {
			return arrayList.size();
		}

		@Override
		public Object getItem(int position) {
			return arrayList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder=null;
			if (convertView == null) {
				holder=new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(context);
				
					convertView = inflater.inflate(R.layout.item_daijinquan_guoqi,
							null);
					holder.name=(TextView) convertView.findViewById(R.id.name);
					holder.xianzhi=(TextView) convertView.findViewById(R.id.xianzhi);
					holder.price=(TextView) convertView.findViewById(R.id.price);
					holder.use_time=(TextView) convertView.findViewById(R.id.use_time);
					convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			holder.name.setText(arrayList.get(position).getName());
			holder.xianzhi.setText(arrayList.get(position).getUse_condition());
			holder.price.setText(arrayList.get(position).getMoney());
			System.out.println(arrayList.get(position).getExpire_time());
			holder.use_time.setText(new SimpleDateFormat("yyyy年MM月dd日").format(Long.parseLong(arrayList.get(position).getExpire_time())*1000));
			return convertView;
		}
		
		public class ViewHolder{
			TextView name,xianzhi,price,use_time;
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		
		old_red_bag_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if (arg2 == adapter.getCount() - 1) {
					Log.d("hu", "linghongbao");
				}

			}
		});
		ResultEntity entity=handlerError(data);
		arrayList=(ArrayList<RedBagModel>) entity.getData();
		adapter = new MyAdapter(getActivity(), arrayList);
		old_red_bag_list.setAdapter(adapter);
		//ByAlert.alert(entity.getData()+"");
	}

}
